/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.*;
import com.cashmate.hibernate.pojo.*;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.commons.io.FileUtils;
import javax.servlet.ServletContext;
import org.apache.struts2.convention.annotation.InterceptorRef;

/**
 *
 * @author Dine
 */
public class UpdateAF extends ActionSupport implements ServletRequestAware {

    private Borrowers bor;
    private String loanappform;
    private BorrowerDAO bord = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private BranchDAO brd = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private EmployeeDAO emd = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private AreaDAO ard = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    private RequirementDAO reqd = (RequirementDAO) ServletActionContext.getServletContext().getAttribute("requirementDAO");
    private ProductDAO pd = (ProductDAO) ServletActionContext.getServletContext().getAttribute("productDAO");
    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private LoanapplicationDAO loanappDAO = (LoanapplicationDAO) ServletActionContext.getServletContext().getAttribute("loanapplicationDAO");
    //Loanver
    private String loanverid;
    //Borrowers
    private String selectedBranch, selectedStatus, actionstatus, borid, selectedArea;
    private String firstname, middlename, lastname, religion, citizenship, streetaddress, barangay, cityormunicipality, province,
            postalcode, age, placeofbirth, dateofbirth, contactno;
    //Loanapplications
    private String amountrequested, suggestedamount, amortizationdays, releasedate,
            provincialstreetaddress, provincialbarangay, provincialcityormunicipality, provincialprovince,
            provincialpostalcode, presentaddressstatus, provincialaddressstatus, spousefirstname, spousemiddlename,
            spouselastname, spousedateofbirth, spouseplaceofbirth, spousereligion, spousecitizenship,
            spouseprovincialstreetaddress, spouseprovincialbarangay, spouseprovincialcityormunicipality, spouseprovincialprovince, spousepostalcode,
            coborrowerfirstname, coborrowermiddlename, coborrowerlastname, coborrowerstreetaddress, coborrowerbarangay,
            coborrowercityormunicipality, coborrowerprovince, coborrowerpostalcode, coborrowerreligion, status,
            remarks, employees, documentno,
            documenttype, documentdate, presentaddresslengthofstayinyears, presentaddresslengthofstayinmonths, provincialaddresslengthofstayinyears,
            provincialaddresslengthofstayinmonths, loantype;
    private int id;
    //Businesses
    private String[] businessname,
            businesstype,
            businessstreetaddress,
            businessbarangay,
            businesscityormunicipality,
            businessprovince,
            businesspostalcode,
            businessstability;
    //Monthlyincome
    private String business, employement, spouseincome, othersources, rentexpense,
            foodandclothing, education, outstandingloan, totalexpenses, netincomebeforetax, grossincome;
    //Dependents
    private String[] dependentname;
    private String[] dependentage;
    private String[] dependentrelationship;
    //Loanswithotherbanks
    private String[] bankname;
    private String[] principalamt;
    private String[] balance;
    //Characterreferences
    private String[] referencename;
    private String[] referenceaddress;
    private String[] referenceoccupation;
    //
    private String successurl;
    //Requirements
    private String[] requirement;
    //image
    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private HttpServletRequest servletRequest;

    @Action(value = "/update-af", results = {
        @Result(name = "input", location = "/loanapplication.jsp"),
        @Result(name = "success", location = "${successurl}"),
        @Result(name = "error", location = "${successurl}")
    })
    @Override
    public String execute() {
        successurl = "../../loanprofile-loanapp.jsp?cat=appli&id="+id;
        Loanapplications l = null;
        try {
            l = loanappDAO.get(id);
        } catch (Exception e) {
            actionstatus = "Loan application doest not exist anymore.";
            return ERROR;
        }

        l.setStatus("posted");

        if (releasedate != null) {

            l.setReleasedate(releasedate);
            try {
                try {
                    loanappDAO.update(l);
                } catch (Exception e) {
                    actionstatus = "Error in posting release date and status";
                    e.printStackTrace();
                    return ERROR;
                }
            } catch (Exception e) {
                actionstatus = "Error in posting release date and status";
                e.printStackTrace();
                return ERROR;
            }

        }

        if (remarks != null) {
            l.setRemarks(remarks);
            try {
                try {
                    loanappDAO.update(l);
                } catch (Exception e) {
                    actionstatus = "Error in posting release date and status";
                    e.printStackTrace();
                    return ERROR;
                }
            } catch (Exception e) {
                actionstatus = "Error in posting release date and status";
                e.printStackTrace();
                return ERROR;
            }
        }

        if (suggestedamount != null) {
            l.setSuggestedamount(Integer.parseInt(suggestedamount));
            try {
                loanappDAO.update(l);
            } catch (Exception e) {
                actionstatus = "Error in posting release date and status";
                e.printStackTrace();
                return ERROR;
            }
        }

        if (amountrequested != null) {
            l.setAmountrequested(Integer.parseInt(amountrequested));
        }

        if (documentno != null) {
            Documents d = l.getDocuments();
            d.setDocumentno(documentno);
            l.setDocuments(d);
        }

        if (spousedateofbirth != null) {
            l.setApplicationdate(spousedateofbirth);
        }

        if (provincialstreetaddress != null) {
            l.setProvincialstreetaddress(provincialstreetaddress);
        }
        if (provincialbarangay != null) {
            l.setProvincialbarangay(provincialbarangay);
        }
        if (provincialcityormunicipality != null) {
            l.setProvincialcityormunicipality(provincialcityormunicipality);
        }
        if (provincialprovince != null) {
            l.setProvincialprovince(provincialprovince);
        }
        if (provincialpostalcode != null) {
            l.setProvincialpostalcode(provincialpostalcode);
        }
        if (presentaddressstatus != null) {
            l.setPresentaddressstatus(presentaddressstatus);
        }
        if (presentaddresslengthofstayinyears != null) {
            l.setPresentaddresslengthofstayinyears(Integer.parseInt(presentaddresslengthofstayinyears));
        }
        if (presentaddresslengthofstayinmonths != null) {
            l.setPresentaddresslengthofstayinmonths(Integer.parseInt(presentaddresslengthofstayinmonths));
        }
        if (provincialaddressstatus != null) {
            l.setProvincialaddressstatus(provincialaddressstatus);
        }
        if (provincialaddresslengthofstayinyears != null) {
            l.setProvincialaddresslengthofstayinyears(Integer.parseInt(provincialaddresslengthofstayinyears));
        }
        if (provincialaddresslengthofstayinmonths != null) {
            l.setProvincialaddresslengthofstayinmonths(Integer.parseInt(provincialaddresslengthofstayinmonths));
        }
        if (spousefirstname != null) {
            l.setSpousefirstname(spousefirstname);
        }
        if (spousemiddlename != null) {
            l.setSpousemiddlename(spousemiddlename);
        }
        if (spouselastname != null) {
            l.setSpouselastname(spouselastname);
        }
        if (spouseplaceofbirth != null) {
            l.setSpouseplaceofbirth(spouseplaceofbirth);
        }
        if (spousereligion != null) {
            l.setSpousereligion(spousereligion);
        }
        if (spousecitizenship != null) {
            l.setSpousecitizenship(spousecitizenship);
        }
        if (spouseprovincialstreetaddress != null) {
            l.setSpouseprovincialstreetaddress(spouseprovincialstreetaddress);
        }
        if (spouseprovincialbarangay != null) {
            l.setSpouseprovincialbarangay(spouseprovincialbarangay);
        }
        if (spouseprovincialcityormunicipality != null) {
            l.setProvincialcityormunicipality(provincialcityormunicipality);
        }
        if (spouseprovincialprovince != null) {
            l.setSpouseprovincialprovince(spouseprovincialprovince);
        }
        if (spousepostalcode != null) {
            l.setSpouseprovincialprovince(spousepostalcode);
        }

        if (coborrowerfirstname != null) {
            l.setCoborrowerfirstname(coborrowerfirstname);
        }
        if (coborrowermiddlename != null) {
            l.setCoborrowermiddlename(coborrowermiddlename);
        }
        if (coborrowerlastname != null) {
            l.setCoborrowerlastname(coborrowerlastname);
        }
        if (coborrowerstreetaddress != null) {
            l.setCoborrowerstreetaddress(coborrowerstreetaddress);
        }
        if (coborrowerbarangay != null) {
            l.setCoborrowerbarangay(coborrowerbarangay);
        }
        if (coborrowercityormunicipality != null) {
            l.setCoborrowercityormunicipality(coborrowercityormunicipality);
        }
        if (coborrowerprovince != null) {
            l.setCoborrowerprovince(coborrowerprovince);
        }
        if (coborrowerpostalcode != null) {
            l.setCoborrowerpostalcode(coborrowerpostalcode);
        }
        if (coborrowerreligion != null) {
            l.setCoborrowerreligion(coborrowerreligion);
        }

        try {
            loanappDAO.update(l);
            actionstatus = "Application form posted.";
            return SUCCESS;
        } catch (Exception e) {
            actionstatus = "Error Updating Application Form.";
            e.printStackTrace();
            return ERROR;
        }

    }

    public void setReleasedate(String releasedate) {
        this.releasedate = releasedate;
    }

    public void setRequirement(String[] requirement) {
        this.requirement = requirement;
    }

    public void setAmortizationdays(String amortizationdays) {
        this.amortizationdays = amortizationdays;
    }

    public void setSuggestedamount(String suggestedamount) {
        this.suggestedamount = suggestedamount;
    }

    public void setContactno(String contactno) {
        this.contactno = contactno;
    }

    public void setSelectedArea(String selectedArea) {
        this.selectedArea = selectedArea;
    }

    public String getSuccessurl() {
        return successurl;
    }

    public void setBusinessbarangay(String[] businessbarangay) {
        this.businessbarangay = businessbarangay;
    }

    public void setBusinesscityormunicipality(String[] businesscityormunicipality) {
        this.businesscityormunicipality = businesscityormunicipality;
    }

    public void setBusinessname(String[] businessname) {
        this.businessname = businessname;
    }

    public void setBusinesspostalcode(String[] businesspostalcode) {
        this.businesspostalcode = businesspostalcode;
    }

    public void setBusinessprovince(String[] businessprovince) {
        this.businessprovince = businessprovince;
    }

    public void setBusinessstability(String[] businessstability) {
        this.businessstability = businessstability;
    }

    public void setBusinessstreetaddress(String[] businessstreetaddress) {
        this.businessstreetaddress = businessstreetaddress;
    }

    public void setBusinesstype(String[] businesstype) {
        this.businesstype = businesstype;
    }

    public void setReferenceaddress(String[] referenceaddress) {
        this.referenceaddress = referenceaddress;
    }

    public void setReferencename(String[] referencename) {
        this.referencename = referencename;
    }

    public void setReferenceoccupation(String[] referenceoccupation) {
        this.referenceoccupation = referenceoccupation;
    }

    public void setBalance(String[] balance) {
        this.balance = balance;
    }

    public void setBankname(String[] bankname) {
        this.bankname = bankname;
    }

    public void setPrincipalamt(String[] principalamt) {
        this.principalamt = principalamt;
    }

    public void setDependentage(String[] dependentage) {
        this.dependentage = dependentage;
    }

    public void setDependentname(String[] dependentname) {
        this.dependentname = dependentname;
    }

    public void setDependentrelationship(String[] dependentrelationship) {
        this.dependentrelationship = dependentrelationship;
    }

    public void setBorid(String borid) {
        this.borid = borid;
    }

    public String getLoanappform() {
        return loanappform;
    }

    public void setLoanappform(String loanappform) {
        this.loanappform = loanappform;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public void setBarangay(String barangay) {
        this.barangay = barangay;
    }

    public void setCitizenship(String citizenship) {
        this.citizenship = citizenship;
    }

    public void setCityormunicipality(String cityormunicipality) {
        this.cityormunicipality = cityormunicipality;
    }

    public void setDateofbirth(String dateofbirth) {
        this.dateofbirth = dateofbirth;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setMiddlename(String middlename) {
        this.middlename = middlename;
    }

    public void setPlaceofbirth(String placeofbirth) {
        this.placeofbirth = placeofbirth;
    }

    public void setPostalcode(String postalcode) {
        this.postalcode = postalcode;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public void setReligion(String religion) {
        this.religion = religion;
    }

    public void setSelectedBranch(String selectedBranch) {
        this.selectedBranch = selectedBranch;
    }

    public void setSelectedStatus(String selectedStatus) {
        this.selectedStatus = selectedStatus;
    }

    public void setStreetaddress(String streetaddress) {
        this.streetaddress = streetaddress;
    }

    public void setActionstatus(String actionstatus) {
        this.actionstatus = actionstatus;
    }

    public String getActionstatus() {
        return actionstatus;
    }

    public Borrowers getBor() {
        return bor;
    }

    public void setBor(Borrowers bor) {
        this.bor = bor;
    }

    public void setAmountrequested(String amountrequested) {
        this.amountrequested = amountrequested;
    }

    public void setCoborrowerreligion(String coborrowerreligion) {
        this.coborrowerreligion = coborrowerreligion;
    }

    public void setCoborrowerbarangay(String coborrowerbarangay) {
        this.coborrowerbarangay = coborrowerbarangay;
    }

    public void setCoborrowercityormunicipality(String coborrowercityormunicipality) {
        this.coborrowercityormunicipality = coborrowercityormunicipality;
    }

    public void setCoborrowerfirstname(String coborrowerfirstname) {
        this.coborrowerfirstname = coborrowerfirstname;
    }

    public void setCoborrowerlastname(String coborrowerlastname) {
        this.coborrowerlastname = coborrowerlastname;
    }

    public void setCoborrowermiddlename(String coborrowermiddlename) {
        this.coborrowermiddlename = coborrowermiddlename;
    }

    public void setCoborrowerpostalcode(String coborrowerpostalcode) {
        this.coborrowerpostalcode = coborrowerpostalcode;
    }

    public void setCoborrowerprovince(String coborrowerprovince) {
        this.coborrowerprovince = coborrowerprovince;
    }

    public void setCoborrowerstreetaddress(String coborrowerstreetaddress) {
        this.coborrowerstreetaddress = coborrowerstreetaddress;
    }

    public void setDocumentdate(String documentdate) {
        this.documentdate = documentdate;
    }

    public void setDocumentno(String documentno) {
        this.documentno = documentno;
    }

    public void setDocumenttype(String documenttype) {
        this.documenttype = documenttype;
    }

    public void setEmployees(String employees) {
        this.employees = employees;
    }

    public void setPresentaddresslengthofstayinmonths(String presentaddresslengthofstayinmonths) {
        this.presentaddresslengthofstayinmonths = presentaddresslengthofstayinmonths;
    }

    public void setPresentaddresslengthofstayinyears(String presentaddresslengthofstayinyears) {
        this.presentaddresslengthofstayinyears = presentaddresslengthofstayinyears;
    }

    public void setPresentaddressstatus(String presentaddressstatus) {
        this.presentaddressstatus = presentaddressstatus;
    }

    public void setProvincialaddresslengthofstayinmonths(String provincialaddresslengthofstayinmonths) {
        this.provincialaddresslengthofstayinmonths = provincialaddresslengthofstayinmonths;
    }

    public void setProvincialaddresslengthofstayinyears(String provincialaddresslengthofstayinyears) {
        this.provincialaddresslengthofstayinyears = provincialaddresslengthofstayinyears;
    }

    public void setProvincialaddressstatus(String provincialaddressstatus) {
        this.provincialaddressstatus = provincialaddressstatus;
    }

    public void setProvincialbarangay(String provincialbarangay) {
        this.provincialbarangay = provincialbarangay;
    }

    public void setProvincialcityormunicipality(String provincialcityormunicipality) {
        this.provincialcityormunicipality = provincialcityormunicipality;
    }

    public void setProvincialpostalcode(String provincialpostalcode) {
        this.provincialpostalcode = provincialpostalcode;
    }

    public void setProvincialprovince(String provincialprovince) {
        this.provincialprovince = provincialprovince;
    }

    public void setProvincialstreetaddress(String provincialstreetaddress) {
        this.provincialstreetaddress = provincialstreetaddress;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public void setSpousecitizenship(String spousecitizenship) {
        this.spousecitizenship = spousecitizenship;
    }

    public void setSpousedateofbirth(String spousedateofbirth) {
        this.spousedateofbirth = spousedateofbirth;
    }

    public void setSpousefirstname(String spousefirstname) {
        this.spousefirstname = spousefirstname;
    }

    public void setSpouselastname(String spouselastname) {
        this.spouselastname = spouselastname;
    }

    public void setSpousemiddlename(String spousemiddlename) {
        this.spousemiddlename = spousemiddlename;
    }

    public void setSpouseplaceofbirth(String spouseplaceofbirth) {
        this.spouseplaceofbirth = spouseplaceofbirth;
    }

    public void setSpousepostalcode(String spousepostalcode) {
        this.spousepostalcode = spousepostalcode;
    }

    public void setSpouseprovincialbarangay(String spouseprovincialbarangay) {
        this.spouseprovincialbarangay = spouseprovincialbarangay;
    }

    public void setSpouseprovincialcityormunicipality(String spouseprovincialcityormunicipality) {
        this.spouseprovincialcityormunicipality = spouseprovincialcityormunicipality;
    }

    public void setSpouseprovincialprovince(String spouseprovincialprovince) {
        this.spouseprovincialprovince = spouseprovincialprovince;
    }

    public void setSpouseprovincialstreetaddress(String spouseprovincialstreetaddress) {
        this.spouseprovincialstreetaddress = spouseprovincialstreetaddress;
    }

    public void setSpousereligion(String spousereligion) {
        this.spousereligion = spousereligion;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setBusiness(String business) {
        this.business = business;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public void setEmployement(String employement) {
        this.employement = employement;
    }

    public void setFoodandclothing(String foodandclothing) {
        this.foodandclothing = foodandclothing;
    }

    public void setGrossincome(String grossincome) {
        this.grossincome = grossincome;
    }

    public void setNetincomebeforetax(String netincomebeforetax) {
        this.netincomebeforetax = netincomebeforetax;
    }

    public void setOthersources(String othersources) {
        this.othersources = othersources;
    }

    public void setOutstandingloan(String outstandingloan) {
        this.outstandingloan = outstandingloan;
    }

    public void setRentexpense(String rentexpense) {
        this.rentexpense = rentexpense;
    }

    public void setSpouseincome(String spouseincome) {
        this.spouseincome = spouseincome;
    }

    public void setTotalexpenses(String totalexpenses) {
        this.totalexpenses = totalexpenses;
    }

    public File getUserImage() {
        return userImage;
    }

    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }

    public String getUserImageContentType() {
        return userImageContentType;
    }

    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }

    public String getUserImageFileName() {
        return userImageFileName;
    }

    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }

    public void setLoantype(String loantype) {
        this.loantype = loantype;
    }

    public void setLoanverid(String loanverid) {
        this.loanverid = loanverid;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
    }
}
